package com.youxin.cpcenter.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.youxin.cpcenter.common.log.LogMethod;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.youxin.busfoundation.bean.wexin.WxOpenAccessTokenBean;
import com.youxin.busfoundation.bean.wexin.WxOpenUserInfoBean;
import com.youxin.busfoundation.bean.wexin.WxPublicTokenBean;
import com.youxin.busfoundation.bean.wexin.WxPublicTokenExpiresBean;
import com.youxin.busfoundation.bean.wexin.WxPublicUserInfoBean;
import com.youxin.busfoundation.utils.WexinUtils;
import com.youxin.cpcenter.bean.RequestWxOpenBean;
import com.youxin.cpcenter.common.Consts;
import com.youxin.foundation.security.Sha1Utils;

/**
 * 微信开放平台登录
 * 
 * @author yingde.cao
 *
 */
@RestController
public class WxOpenController {

	@LogMethod(id="ent.wxopen.callback",eventContent={})
	@RequestMapping("/wxopen/callback")
	public String wxOpenCallBack(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			RequestWxOpenBean reqeustBean) {
		String code = reqeustBean.getCode();
		Objects.requireNonNull(code, "Code为空");
		
		WxPublicTokenExpiresBean accessTokenBean = WexinUtils.getPublicToken(Consts.WX_PUBLIC_APPID,
				Consts.WX_PUBLIC_SECRET, code);
		
		WxPublicUserInfoBean userInfoBean = WexinUtils.getPublicUserInfo(accessTokenBean.getAccessToken(),
				accessTokenBean.getOpenid());
		return "log";
	}

	/**
	 * 排序方法
	 *
	 * @param token
	 * @param timestamp
	 * @param nonce
	 * @return
	 */
	public static String sort(String token, String timestamp, String nonce) {
		String[] strArray = { token, timestamp, nonce };
		Arrays.sort(strArray);

		StringBuilder sbuilder = new StringBuilder();
		for (String str : strArray) {
			sbuilder.append(str);
		}

		return sbuilder.toString();
	}

	@LogMethod(id="ent.wxopen.token",eventContent={})
	@RequestMapping("/wxopen/token")
	public String wxOpenLogin(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
			WxPublicTokenBean tokenBean) {
		Objects.requireNonNull(tokenBean, "参数为空");
		Objects.requireNonNull(tokenBean.getSignature(), "参数token为空");
		Objects.requireNonNull(tokenBean.getNonce(), "参数nonce为空");
		Objects.requireNonNull(tokenBean.getEchostr(), "参数echostr为空");
		Objects.requireNonNull(tokenBean.getTimestamp(), "参数timestamp为空");
		// 排序
		String sortString = sort(Consts.WX_PUBLIC_TOKEN, tokenBean.getTimestamp(), tokenBean.getNonce());
		// 加密
		String encrypted = Sha1Utils.sha1(sortString);

		if(encrypted.equalsIgnoreCase(tokenBean.getSignature()))
		{
			return tokenBean.getEchostr();
		}
		return "";
	}

}
